<?php
/**
 * ValoresDefaultDAO
 *
 * @since 09/10/2009
 */
class ValoresDefaultDAO{

    /**
     * Get Domain object by primry key
     *
     * @param String $id primary key
     * @return ValoresDefaultDAO
     */
    public function load($id){
            $sql = 'SELECT * FROM valoresdefault WHERE codCampo = ?';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($id);
            return $this->getRow($sqlQuery);
    }

    /**
	* Delete record from table
	* @param campo primary key
	*/
	public function deleteByCampo($codCampo)
	{
		$sql = 'DELETE FROM valoresdefault WHERE codCampo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($codCampo);
	 	return $this->execute($sqlQuery);
 	}


    /**
     * Get all records from table
     */
    public function queryAll(){
            $sql = 'SELECT * FROM valoresdefault';
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

    /**
     * Get all records from table ordered by field
     *
     * @param $orderColumn column name
     */
    public function queryAllOrderBy($orderColumn){
            $sql = 'SELECT * FROM valoresdefault ORDER BY '.$orderColumn;
            $sqlQuery = new SqlQuery($sql);
            return $this->getList($sqlQuery);
    }

	/**
     * Get all records from table by codCampo
     */
    public function queryByCampo($valor)
    {
            $sql = 'SELECT * FROM valoresdefault WHERE codCampo = ? LIMIT 1';
            $sqlQuery = new SqlQuery($sql);
            $sqlQuery->setNumber($valor);
            return $this->getRow($sqlQuery);
    }

    /**
     * Read row
     *
     * @return FormularioMySql
     */
    protected function readRow($row) {
            $valoresDefault = new ValoresDefault();

            $valoresDefault->codCampo = $row['codCampo'];
            $valoresDefault->valorDefault = $row['valorDefault'];

            return $valoresDefault;
    }

    protected function getList($sqlQuery) {
            $tab = QueryExecutor :: execute($sqlQuery);
            $ret = array ();
            for ($i = 0; $i < count($tab); $i++) {
                    $ret[$i] = $this->readRow($tab[$i]);
            }
            return $ret;
    }

    /**
     * Get row
     *
     * @return valoresDefault
     */
    protected function getRow($sqlQuery) {
            $tab = QueryExecutor :: execute($sqlQuery);
            return $this->readRow($tab[0]);
    }

    /**
     * Execute sql query
     */
    protected function execute($sqlQuery) {
            return QueryExecutor :: execute($sqlQuery);
    }

    /**
     * Execute sql query
     */
    protected function executeUpdate($sqlQuery) {
            return QueryExecutor :: executeUpdate($sqlQuery);
    }

    /**
     * Insert row to table
     */
    protected function executeInsert($sqlQuery) {
            return QueryExecutor :: executeInsert($sqlQuery);
    }

    /**
     * Insert record to table
     *
     * @param ValoresDefaultMySql valoresdefault
     */
    public function insert($valoresDefault){
        $sql = 'INSERT INTO valoresdefault (codCampo, valorDefault) VALUES (?, ?)';
        $sqlQuery = new SqlQuery($sql);

        $sqlQuery->set($valoresDefault->codCampo);
        $sqlQuery->set($valoresDefault->valorDefault);

        $this->executeInsert($sqlQuery);
    }

}
?>
